<?php

class DCfComposer extends DActiveRecord {

    private $curPage = 0;
    private $defaultSort;

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public function tableName() {
        return 'cf_composer';
    }

    public function rules() {
        return array(
			array('name', 'required'),
			array('name', 'ext.validators.DupplicateValidator',
                'tableName'   => $this->tableName(),
                'isNewRecord' => $this->isNewRecord,
                'currId'      => $this->id,
            ),
			array('name, title_seo', 'length', 'max'=>255),
			array('id, name', 'safe', 'on'=>'search'),
        );
    }

    public function relations() {
        return array();
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'name' => 'Tên nhạc sĩ',
        );
    }

    public function search() {
        //Restore state from session
        $this->restoreState();

        //Criteria
        $criteria = new CDbCriteria;
        $criteria->compare('name', $this->name, true);

        //Sorting
        $sort               = new CSort();
        $sort->defaultOrder = ($this->defaultSort == '') ? array('id' => true) : $this->defaultSort;
        $sort->attributes = array(
            'id' => array(
                'asc'       => 'id ASC',
                'desc'      => 'id DESC',
                'default'   => 'desc',
            ),
            'name' => array(
                'asc'      => 'name ASC',
                'desc'     => 'name DESC',
                'default'  => 'asc',
            ),
        );
        $sort->applyOrder($criteria);

        return new CActiveDataProvider($this, array(
            'criteria'   => $criteria,
            'sort'       => $sort,
            'pagination' => array('currentPage' => $this->curPage),
        ));
    }

    protected function beforeSave() {
        parent::beforeSave();
        
        //title_seo
        $titleSeo = MyHelpers::getInstance()->convertSeoStr($this->name);
        $this->setAttribute('title_seo', $titleSeo);

        return true;
    }
    
    private function restoreState() {
        $state = SpsState::getState();

        if ($state) {
            if (isset($state['sort']))
                $this->defaultSort = $state['sort'];
            if (isset($state['page']))
                $this->curPage     = $state['page'];

            if (isset($state['search'])) {
                foreach ($state['search'] as $key => $value)
                    if ($value != '')
                        $this->$key = $value;
            }
        }
    }
    
    public function getList($active = '') {
        $criteria = new CDbCriteria(array(
            'order' => 'name COLLATE \'utf8_unicode_ci\' ASC',
        ));
        if ($active != '') {
            $criteria->compare('activated_flag', $active);
        }

        $data   = $this->findAll($criteria);
        $result = array();

        foreach ($data as $row) {
            $result[$row['id']] = $row['name'];
        }
        
//        asort($result);
//        MyHelpers::getInstance()->log($result, true);
        
        return $result;
    }

}